<script setup lang="ts">
import VDialog from "@/components/VDialog/VDialog.vue";
import { computed, reactive, ref } from "vue";
import {
  InspectionCheckDTO,
  addInspectionCheckApi,
  updateInspectionCheckApi, AddOrUpdateInspectionCheckCommand
} from "@/api/airport/inspection/inspectionCheck";
import { ElMessage, FormInstance, FormRules } from "element-plus";

interface Props {
  type: "add" | "update";
  modelValue: boolean;
  row?: InspectionCheckDTO;
}

const props = defineProps<Props>();
const emits = defineEmits<{
  (e: "update:modelValue", v: boolean): void;
  (e: "success"): void;
}>();

const visible = computed({
  get: () => props.modelValue,
  set(v) {
    emits("update:modelValue", v);
  }
});

const formData = reactive<AddOrUpdateInspectionCheckCommand | InspectionCheckDTO>({
  inspectionId: 0,
  selfcheckId: undefined,
  inspectionUserId: undefined,
  inspectionResult: "",
  inspectionQuestion: "",
  inspectionDeadlineTime: "",
  rectificationRequire: "",
  directCause: "",
  fundamentalCause: "",
  correctMeasure: "",
  correctMeasureDeadlineTime: "",
  correctMeasureDealTime: "",
  correctMeasureDealPeople: "",
  correctMeasureResult: "",
  preventionMeasure: "",
  preventionMeasureDeadlineTime: "",
  preventionMeasureDealTime: "",
  preventionMeasureDealPeople: "",
  preventionMeasureResult: "",
  remark: "",
  dealType: "",
  relationId: undefined,
  relationType: "",
  startTime: "",
  endTime: "",
  feedback: "",
  checkTermId: undefined,
  checkStandardId: undefined,
  checkTermCode: "",
  checkStandardCode: "",
  checkOpinion: "",
  checkSign: "",
  inspectionDeptId: undefined,
  status: undefined,
  dealResult: ""
});

const rules: FormRules = {
  inspectionId: [
    {
      required: true,
      message: "检查计划ID不能为空"
    }
  ],
  inspectionResult: [
    {
      required: true,
      message: "检查结果不能为空"
    }
  ],
  checkTermCode: [
    {
      required: true,
      message: "检查项编码不能为空"
    }
  ],
  checkStandardCode: [
    {
      required: true,
      message: "检查标准编码不能为空"
    }
  ]
};
const formRef = ref<FormInstance>();
function handleOpened() {
  if (props.row) {
    Object.assign(formData, props.row);
  } else {
    formRef.value?.resetFields();
  }
}

const loading = ref(false);
async function handleConfirm() {
  try {
    loading.value = true;
    if (props.type === "add") {
      await addInspectionCheckApi(formData as AddOrUpdateInspectionCheckCommand);
    } else if (props.type === "update") {
      await updateInspectionCheckApi(formData as InspectionCheckDTO);
    }
    ElMessage.info("提交成功");
    visible.value = false;
    emits("success");
  } catch (e) {
    console.error(e);
    ElMessage.error((e as Error)?.message || "提交失败");
  } finally {
    loading.value = false;
  }
}
</script>

<template>
  <v-dialog
    show-full-screen
    :fixed-body-height="false"
    use-body-scrolling
    :title="type === 'add' ? '新增检查项' : '更新检查项'"
    v-model="visible"
    :loading="loading"
    @confirm="handleConfirm"
    @cancel="visible = false"
    @opened="handleOpened"
  >
    <el-form :model="formData" label-width="120px" :rules="rules" ref="formRef">
      <el-form-item prop="inspectionId" label="检查计划ID" required inline-message>
        <el-input v-model="formData.inspectionId" />
      </el-form-item>
      <el-form-item prop="inspectionResult" label="检查结果" required>
        <el-input v-model="formData.inspectionResult" />
      </el-form-item>
      <el-form-item prop="checkTermCode" label="检查项编码" required>
        <el-input v-model="formData.checkTermCode" />
      </el-form-item>
      <el-form-item prop="checkStandardCode" label="检查标准编码" required>
        <el-input v-model="formData.checkStandardCode" />
      </el-form-item>
      <el-form-item prop="startTime" label="检查开始时间">
        <el-date-picker
          v-model="formData.startTime"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="选择日期时间"
        />
      </el-form-item>
      <el-form-item prop="endTime" label="检查结束时间">
        <el-date-picker
          v-model="formData.endTime"
          type="datetime"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="选择日期时间"
        />
      </el-form-item>
      <el-form-item prop="inspectionQuestion" label="检查问题">
        <el-input type="textarea" v-model="formData.inspectionQuestion" />
      </el-form-item>
      <el-form-item prop="remark" label="备注" style="margin-bottom: 0">
        <el-input type="textarea" v-model="formData.remark" />
      </el-form-item>
    </el-form>
  </v-dialog>
</template>
